<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport"
        content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <link rel="stylesheet" type="text/css" href="../css/aui.2.0.css" />
</head>

<body>
    <header class="aui-bar aui-bar-nav" style="background-color: #03a9f4;position:fixed;" id="header">
        <a class="aui-pull-left aui-btn" tapmode onclick="closeWin();">
            <span class="aui-iconfont aui-icon-left"></span>
        </a>
        <div class="aui-title" id="title">
            工单未达标原因
        </div>
    </header>

    <div class="aui-content aui-margin-b-15" style="margin-top:50px;">
        <ul class="aui-list aui-form-list">
          <li class="aui-list-item">
            <div class="aui-list-item-inner">
              <div class="aui-list-item-label">未达标原因</div>
              <div class="aui-list-item-input">
                <select id="reason" onchange="showHideItems()"></select>
              </div>
            </div>
          </li>

          <li class="aui-list-item" id="05" style="display: none;">
            <div class="aui-list-item-inner">
              <div class="aui-list-item-label">备注</div>
              <div class="aui-list-item-input">
                <textarea id="remark"></textarea>
              </div>
            </div>
          </li>
          
          <li class="aui-list-item" id="01" style="display: none;">
            <div class="aui-list-item-inner">
              <div class="aui-list-item-label">物料</div>
              <div class="aui-list-item-input">
                <select id="mrlCode" multiple="multiple"></select>
              </div>
            </div>
          </li>
        </ul>
        <div style="width:100%;margin-top:2%;"></div>
        <div class="aui-btn aui-btn-info aui-btn-block" onclick="save()">保存</div>
      </div>
      
</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/doT.min.js"></script>
<script type="text/javascript" src="../script/public.js"></script>
<script type="text/javascript" src="../script/hammer.min.js"></script>
<script type="text/javascript" src="../script/jquery-1.8.2.min.js"></script>
<script type="text/javascript">

    var workOrderCode = null;
    apiready = function () {

        workOrderCode = api.pageParam.title;

        getNoStandard();
        getMrlcode();
        showHideItems();
    };

    function showHideItems() {
      // 获取下拉框的值
      var selectedValue = document.getElementById("reason").value;

      var selectedItem01 = document.getElementById("01");
      var selectedItem05 = document.getElementById("05");

      if(selectedValue == "01"){
        if (selectedItem01) {
            selectedItem01.style.display = "block";
        }
        if (selectedItem05) {
            selectedItem05.style.display = "none";
        }
      }else if(selectedValue == "05"){
        if (selectedItem05) {
            selectedItem05.style.display = "block";
        }
        if (selectedItem01) {
            selectedItem01.style.display = "none";
        }
      }else{
        if (selectedItem01) {
            selectedItem01.style.display = "none";
        }
        if (selectedItem05) {
            selectedItem05.style.display = "none";
        }
      }

    }

    //查询未达标原因
    function getNoStandard() {

        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/reportNoCompleteReason";
        var data = {
            values: {
                appVersion: api.appVersion,
                un: un,
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 30,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                if (myobj.code == '0') {
                    var list = eval(myobj.list);
                    chkList = list;
                    var html = "";
                    for (var i = 0; i < list.length; i++) {
                        html += "<option value='" + list[i].value + "'>" + list[i].label + "</option>";
                    }
                    $api.html($api.byId("reason"), html);
                } else {
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            }
        });
    }

        //查询未达标原因
        function getMrlcode() {

            var un = localStorage.getItem('un');
            var serverUrl = localStorage.getItem('serverUrl');
            var url = serverUrl + "/app/reportCompleteGetBom";
            var data = {
                values: {
                    un: un,
                    appVersion: api.appVersion,
                    workOrderCode:workOrderCode,
                }
            };
            api.ajax({
                url: url,
                method: 'post',
                dataType: 'json',
                timeout: 30,
                returnAll: false,
                data: data
            }, function (ret, err) {
                if (ret) {
                    var data = JSON.stringify(ret);
                    var myobj = eval("(" + data + ")");
                    if (myobj.code == '0') {
                        var list = eval(myobj.list);
                        chkList = list;
                        var html = "";
                        for (var i = 0; i < list.length; i++) {
                            html += "<option value='" + list[i].value + "'>" + list[i].label + "</option>";
                        }
                        $api.html($api.byId("mrlCode"), html);
                    } else {
                        api.toast({
                            msg: myobj.message,
                            duration: 3000
                        });
                    }
                }
            });
        }

    //选则返回函数
    function reportNoStandard() {
        api.closeWin();
        var name = api.pageParam.name;

        api.execScript({
            name: name,
            script: 'reportNoStandard()'
        });
    }

    //返回按钮
    function closeWin() {
        api.closeWin();
    }


    function save() {
        var reason = $("#reason").val();
        var mrlCode = $("#mrlCode").val();
        var remark = $("#remark").val();
        if (reason == undefined || reason == null || reason == "") {
            api.toast({
                msg: '未选择原因无法保存！',
                duration: 3000
            });
            return;
        }
        var selectElement = document.getElementById("mrlCode");

        if (reason == '01' && selectElement.selectedOptions.length === 0) {
            // 未选择任何选项，即为空
            api.toast({
                msg: '未选择物料无法保存！',
                duration: 3000
            });
            return;
        }
        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/reportComplete";
        var data = {
            values: {
                workOrderCode : workOrderCode,
                mrlCode : mrlCode,
                appVersion: api.appVersion,
                reason : reason,
                remark: remark,
                noStandard : 'true',
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 30,
            returnAll: false,
            data: data
        }, function(ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                if (myobj.code == "0") {
                    alert("完工成功");
                    // api.toast({
                    //     msg: '完工成功',
                    //     duration: 3000,
                    //     location: 'bottom'
                    // });
                    api.closeWin();
                } else if (myobj.code == "1") {
                    alert(myobj.message);
                }
            } else {
                alert(JSON.stringify(err));
            }
        });
    }
    

</script>

</html>